package superman.app.gr100.biz.gr_expapp;

import android.content.Context;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Objects;

import iclass.Itools;
import  superman.app.gr100.biz.gr_expapp.Login;

/**
 * Created by Scor on 2015/12/2.
 */
public class OrderBowerAdapter extends ArrayAdapter implements View.OnClickListener {
    private String orderState;
    private String payWay;
    private List<Order> olist;
    private int resourceId;
    private Order order;
    private Itools itools = new Itools();
    public OrderBowerAdapter(Context context, int resource, List<Order> objects) {
        super(context, resource,objects);
        resourceId = resource;
        olist = objects;
    }
    @Override
    public View getView(final int position,View convertView,ViewGroup prent){
        order = (Order) getItem(position);
        View view;
        if(convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resourceId,null);
        }else {
            view = convertView;
        }
        TextView server_name = (TextView) view.findViewById(R.id.server_name);
        TextView order_state = (TextView) view.findViewById(R.id.order_state);
        TextView take_address = (TextView) view.findViewById(R.id.take_address);
        TextView to_address = (TextView) view.findViewById(R.id.to_address);
        TextView book_date = (TextView) view.findViewById(R.id.book_date);
        TextView pay_type = (TextView) view.findViewById(R.id.pay_type);
        TextView order_price = (TextView) view.findViewById(R.id.order_price);
        Button send_button = (Button) view.findViewById(R.id.send_btn);
        send_button.setOnClickListener(this);

        server_name.setText(order.getServerName());
        order_state.setText(getStrState(order.getOrderState()));
        take_address.setText(order.getTakeAddress());
        to_address.setText(order.getToAddress());
        book_date.setText(getDateToString(String.valueOf(order.getBookDate())));
        pay_type.setText(getStrPayWay(order.getPayType()));
        order_price.setText(String.valueOf(order.getOrderPrice()));
        return view;
    }
    @Override
    public void onClick(View v) {
        String url = getContext().getResources().getString(R.string.mission_push_url_post);
        JSONObject mission = new JSONObject();
        mission.put("orderid","O123456789");
        mission.put("partner","scor");
        mission.put("fromAddr","中国scor");
        mission.put("area","B");
        mission.put("toAddr","bana");
        mission.put("toName","Scor");
        mission.put("toMobile","13770683580");
        JSONObject data = new JSONObject();
        data.put("method", "logistics.mission.push");
        data.put("appkey", "111");
        data.put("timestamp", "1111111");
        data.put("param", mission.toString());
        itools.httprequest(url,data);
        OrderBowerAdapter.this.olist.remove(order);
        OrderBowerAdapter.this.notifyDataSetChanged();
        }
    public String getStrState(int i){
        switch (i){
            case 1000:
                orderState = "带付款";
                break;
            case 2000:
                orderState = "待打包";
                break;
            default:
                break;
        }
        return orderState;
    }
    public  String getStrPayWay(String s){
        switch (s){
            case "wxpay":
                payWay = "微信支付：";
                break;
            case "hdpay":
                payWay = "货到付款：";
                break;
            case "yepay":
                payWay = "余额支付：";
                break;
            default:
                break;
        }
        return payWay;
    }
    public static String getDateToString(String time) {
        java.util.Date d = new java.util.Date(Long.parseLong(time));
        SimpleDateFormat s = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return s.format(d);
    }

}